package WorkTask3;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Login {
    private static File file = new File("d:/a.txt");
    public static void main(String[] args) throws Exception {

        List<Student> list = null;
        //初始化的逻辑判断及处理
        if (file.exists()){
            list = Login.read(file);
        }else {
            list = new ArrayList<>();
            //File file = new File("d:/a.txt");//
        }

        xx:while (true){
            System.out.println("欢迎使用学生管理系统！");
            System.out.println("请输入以下选择！");
            System.out.println("1,模拟已有数据学生！");
            System.out.println("2,删除学生！");
            System.out.println("3,查看学生信息！");
            System.out.println("4,修改学生信息！");
            System.out.println("5,查看所有学生！");
            System.out.println("6,添加新学生");
            System.out.println("7,退出系统！");
            Scanner s = new Scanner(System.in);
            int num = s.nextInt();
            //以学生的学号作为基准来进行操作！
          switch (num){
                case 1:{//1，添加学生！
                    try {
                        Login.set(list);
                    } catch (AgeException | IdException e) {
                        e.printStackTrace();
                    }
                    System.out.println("添加成功！");
                    break;
                }
                case 2:{
                    System.out.println("请输入删除学生的学号！");
                    list.remove(s.nextInt());
                    break;
                } case 3:{
                    System.out.println("请输入该学生的学号！");
                    System.out.println(list.get(s.nextInt()));
                    break;
                } case 4:{
                    System.out.println("请输入要修改学生的学号！");
                    int num2 = s.nextInt();
                    System.out.println("您想要修改该学生什么内容？");
                    System.out.println("1,修改年龄 2，修改名字 3，修改学号");
                    int n = s.nextInt();
                    switch (n){
                        case 1:{
                            System.out.println("请输入修改的年龄数据");
                            try {
                                list.get(num2).setAge(s.nextInt());
                            } catch (AgeException e) {
                                e.printStackTrace();
                            }
                            break;
                        } case 2:{
                            System.out.println("请输入修改的名字！");
                            list.get(num2).setName(s.next());
                            break;
                        } case 3:{
                            System.out.println("请输入修改的学号！");
                            try {
                                list.get(num2).setId(s.nextInt());
                            } catch (IdException e) {
                                e.printStackTrace();
                            }
                            break;
                        } default:
                            System.out.println("无此选项！");
                    }
                }
                case 5:{
                    System.out.println(list);
                    break;
                }
                case 6:{
                    try {
                        System.out.println("请输入新添加的学生信息（姓名，学号，年龄）");
                        list.add(new Student(s.next(),s.nextInt(),s.nextInt()));
                    } catch (AgeException | IdException e) {
                        e.printStackTrace();
                    }
                    System.out.println("添加成功！");
                    break;
                }
                case 7:{
                    try {
                        Login.write(list);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("退出系统！");
                    break xx;
                }
                default:
                    System.out.println("无此选项！");
            }

        }

    }
    //用该方法来对学生的信息进行操作，
    public static void set(List<Student> list) throws AgeException, IdException {
        list.add(new Student("小明",0,18));
        list.add(new Student("小红",1,18));
        list.add(new Student("小建",2,19));
        list.add(new Student("小王",3,20));
    }

    public static void write(List<Student> list) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(list);
        oos.close();
    }

    public static List<Student> read(File file) throws IOException, ClassNotFoundException {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            Object o1 = ois.readObject();
            List<Student> list = (List<Student>)o1;
            ois.close();
            return list;
    }


}
